package com.erlang4j.internal.exceptions;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * The basic exception that all Erlang4J exceptions extend.
 * <p>
 * Design Notes
 * <ul>
 * <li>All the Erlang4JExceptions are internationalised. This is because it is easy to code it up this way at the
 * beginning, but hard to add it later!
 * <li>The bundle is hardcoded...but we can change that later :)
 * <li>The exceptions all extend RunTimeException. For reasons as to why you might want to do that see the Spring
 * framework.
 * <li>Currently each exception extends Erlang4JException rather than through a grouping mechanism, As we get more
 * exceptions that is likely to change
 * <li>All the exceptions should be tested as well as the none exceptional behaviour
 * </ul>
 * </p>
 * 
 * @author Phil Rice
 */
public class Erlang4jException extends RuntimeException {
	public static final String bundleName = Erlang4jException.class.getPackage().getName() + ".Erlang4j";

	private final static ResourceBundle bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault(), Erlang4jException.class.getClassLoader());

	public Erlang4jException(String key, Object... parameters) {
		super(makeMessage(key, parameters));
	}

	public Erlang4jException(Throwable t, String key, Object... parameters) {
		super(makeMessage(key, parameters), t);
	}

	private static String makeMessage(String key, Object... parameters) {
		return bundle.containsKey(key) ? MessageFormat.format(bundle.getString(key), parameters) : "Could not find key: " + key;
	}

}
